package com.zjsru.plan2023.oneday;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: CookLee
 * @Date: 2023/5/25
 * 差值数组不同的字符串
 * 输入：words = ["adc","wzy","abc"]
 * 输出："abc"
 * 解释：
 * - "adc" 的差值整数数组是 [3 - 0, 2 - 3] = [3, -1] 。
 * - "wzy" 的差值整数数组是 [25 - 22, 24 - 25]= [3, -1] 。
 * - "abc" 的差值整数数组是 [1 - 0, 2 - 1] = [1, 1] 。
 * 不同的数组是 [1, 1]，所以返回对应的字符串，"abc"。
 *
 */
public class OddString {
    
    /**
     * 模拟
     *
     * @param words 单词
     * @return {@link String}
     */
    public String oddString(String[] words) {
        String ans = "";
        //关联字符串化得到差值和出现次数
        Map<String, Integer> map = new HashMap<>();
        //关联words中的字符串以及字符串化得到差值
        Map<String, String> mapCopy = new HashMap<>();
        for (String word : words) {
            int[] desWord = new int[word.length() - 1];
            String key = "";
            for (int i = 1; i < word.length(); i++) {
                desWord[i - 1] = (word.charAt(i) - 'a') - (word.charAt(i - 1) - 'a');
            }
            key = this.arrayToString(desWord);
            map.put(key, map.getOrDefault(key, 0) + 1);
            mapCopy.put(key, word);
        }
        //使用两个hashMap关联获取数据
        for (Map.Entry<String, Integer> stringIntegerEntry : map.entrySet()) {
            if (stringIntegerEntry.getValue() == 1 || map.size() == 1) {
                ans = mapCopy.get(stringIntegerEntry.getKey());
                break;
            }
        }
        return ans;
    }
    
    /**
     * 数组字符串化助手类
     *
     * @param desWord des词
     * @return {@link String}
     */
    private String arrayToString(int[] desWord) {
        StringBuilder sb = new StringBuilder();
        for (int num:desWord) {
            sb.append(num);
        }
        return sb.toString();
    }
    
    public static void main(String[] args) {
        OddString oddString = new OddString();
        String[] words = new String[] {"abm","bcn","alm"};
        System.out.println(oddString.oddString(words));
    }
}
